﻿using System.Collections.Generic;
using System.Linq;

namespace OpenWaves.Video
{

    public class CompositeVideoWebsiteApi : IVideoWebsiteApi
    {
        private readonly IEnumerable<IVideoWebsiteApi> videoApis;

        public CompositeVideoWebsiteApi(IEnumerable<IVideoWebsiteApi> videoApis)
        {
            this.videoApis = videoApis;
        }

        public IVideo GetVideo(string url)
        {
            IVideoWebsiteApi seletectApi = videoApis.FirstOrDefault(v => v.IsSupported(url));
            
            if (seletectApi == null)
            {
                return null;
            }

            return seletectApi.GetVideo(url);
        }

        public bool IsSupported(string url)
        {
            int supportedApiCount = videoApis.Count(v => v.IsSupported(url));
            return supportedApiCount == 1;
        }
    }
}